package dwz.web.management.lotto;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import dwz.business.lotto.DaiLiHuiyuanInterface;
import dwz.business.lotto.HaoMaLeiXingInterface;
import dwz.business.lotto.QiHaoInterface;
import dwz.business.lotto.ZhuDanInterface;
import dwz.common.util.MD5Util;
import dwz.persistence.beans.DaiLiHuiyuan;
import dwz.persistence.beans.HaoMaLeiXing;
import dwz.persistence.beans.HaoMaLeiXingExample;
import dwz.persistence.beans.QiHao;
import dwz.persistence.beans.QiHaoExample;
import dwz.persistence.beans.ZhuDan;
import dwz.web.BaseController;
import dwz.web.vo.CountBbVO;
import dwz.web.vo.QiHaoVo;
import dwz.web.vo.ZhuDanVo;

/**
 * 后台所有action
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value="/management/houtai")
public class HoutaiAction extends BaseController{
	@Autowired
	private ZhuDanInterface zhuDanInterface;
	@Autowired
	private QiHaoInterface qiHaoInterface;
	@Autowired
	private DaiLiHuiyuanInterface daiLiHuiyuanInterface;
	@Autowired
	private HaoMaLeiXingInterface haoMaLeiXingInterface;
	
	
	
	/**
	 * menu
	 */
	@RequestMapping("/menu")
	public String menu(Model model,HttpServletRequest request){
		DaiLiHuiyuan daiLiHuiyuan = daiLiHuiyuanInterface.selectByPrimaryKey((Integer)request.getSession().getAttribute(CommonFields.USERID));
		if(daiLiHuiyuan.getJb_j()==1){
			model.addAttribute("userName", "总监："+daiLiHuiyuan.getYhm_j());
		}else if(daiLiHuiyuan.getJb_j()==2){
			model.addAttribute("userName", "大股东："+daiLiHuiyuan.getYhm_j());
		}else if(daiLiHuiyuan.getJb_j()==3){
			model.addAttribute("userName", "股东："+daiLiHuiyuan.getYhm_j());
		}else if(daiLiHuiyuan.getJb_j()==4){
			model.addAttribute("userName", "总代理："+daiLiHuiyuan.getYhm_j());
		}else if(daiLiHuiyuan.getJb_j()==5){
			model.addAttribute("userName", "代理："+daiLiHuiyuan.getYhm_j());
		}
		return "/management/back/menu";
	}
	
	/**
	 * 后台首页
	 */
	@RequestMapping("/houtaishouyue")
	public String houtaishouyue(){
		return "/management/back/houtaishouyue";
	}
	/**
	 * 跳转到设置页面
	 * @return
	 */
	@RequestMapping("/shezhi")
	public String  shezhi(){
		return "/management/back/shezhi";
	}
	
	/**
	 * 跳转到下级管理页面
	 * @return
	 */
	@RequestMapping("/xiajiguanli")
	public String  xiajiguanli(){
		return "/management/back/xiajiguanli";
	}
	/**
	 * 跳转到新增会员
	 * @return
	 */
	@RequestMapping("/xinzengxiaji")
	public String  xinzengxiaji(){
		return "/management/back/xinzengxiaji";
	}
	
	
	/**
	 * 跳转到新增下级成功页面
	 * @return
	 */
	@RequestMapping("/xiajixinzengchenggong")
	public String  xiajixinzengchenggong(){
		return "/management/back/xiajixinzengchenggong";
	}
	
	
	/**
	 * 跳转到 信用额度不能超过0页面
	 * @return
	 */
	@RequestMapping("/xinyongedubunengchaoguoling")
	public String  xinyongedubunengchaoguoling(){
		return "/management/back/xinyongedubunengchaoguoling";
	}
	
	
	/**
	 * 跳转到 新增代理页面
	 * @return
	 */
	@RequestMapping("/xinzengdaili")
	public String  xinzengdaili(){
		return "/management/back/xinzengdaili";
	}
	/**
	 * 跳转到 新增代理成功页面
	 * @return
	 */
	@RequestMapping("/xinzengdailichenggong")
	public String  xinzengdailichenggong(){
		return "/management/back/xinzengdailichenggong";
	}
	
	/**
	 * 跳转到 新增会员成功页面
	 * @return
	 */
	@RequestMapping("/xinzenghuiyuanchenggong")
	public String  xinzenghuiyuanchenggong(){
		return "/management/back/xinzenghuiyuanchenggong";
	}
	
	
	
	/**
	 * 跳转到 修改密码页面
	 * @return
	 */
	@RequestMapping("/xiugaimima")
	public String  xiugaimima(Model model,HttpServletRequest request){
		return "/management/back/xiugaimima";
	}
	/**
	 * 修改密码操作
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/xiugaimimaDo")
	public String xiugaimimaDo(Model model,HttpServletRequest request){
		String mmStr = "a12345,ab1234,abc123,a1b2c3,aaa111,123qwe,";
		String oldPwd = request.getParameter("oldpassword");
		String newPwd = request.getParameter("newpassword");
		Integer id = (Integer) request.getSession().getAttribute(CommonFields.USERID);
		DaiLiHuiyuan daiLiHuiyuan = daiLiHuiyuanInterface.selectByPrimaryKey(id);
		if (!MD5Util.MD5(oldPwd).equals(daiLiHuiyuan.getMm_j())) {
			model.addAttribute("message", "原密码不正确");
		}else if(mmStr.indexOf(newPwd+",")!=-1){
			model.addAttribute("message", "系统禁止不可用该密码");
		}else {
			daiLiHuiyuan.setMm_j(MD5Util.MD5(newPwd));
			daiLiHuiyuan.setSfxgmm_j(1);
			try{
			daiLiHuiyuanInterface.updateByPrimaryKeySelective(daiLiHuiyuan);
			request.getSession().setAttribute(CommonFields.ISMODIFYPASSWORD, 1);
			return "/management/back/houtaishouyue";
			}catch (Exception e) {
               model.addAttribute("message", "更新出现错误");
			}
		}
		return "/management/back/xiugaimima";
	}
	
	private QiHao findQiHaoByQh(Integer qh){
		return qiHaoInterface.findQiHaoByQh(qh);
	}
	
	/**
	 * 日报表
	 * @return
	 */
	@RequestMapping("/baobiao")
	public String  baobiao(Model model,HttpServletRequest request){
	
		String url = "" ;
		ZhuDan zhuDan = new ZhuDan();
		DaiLiHuiyuan daiLiHuiyuan = daiLiHuiyuanInterface.selectByPrimaryKey((Integer) request.getSession().getAttribute(CommonFields.USERID));
		if(daiLiHuiyuan.getJb_j()==1){ //总监
		
		}else if(daiLiHuiyuan.getJb_j()==2){ //大股东
 
		}else if(daiLiHuiyuan.getJb_j()==3){ //股东
			zhuDan.setF1("3");
			zhuDan.setF5(daiLiHuiyuan.getId());
		}else if(daiLiHuiyuan.getJb_j()==4){ //总代理
			zhuDan.setF1("4"); //f1用于判断是那种用户
			zhuDan.setF6(2);
			zhuDan.setF5(daiLiHuiyuan.getId());  
			url = "/management/back/baobiao";
		}else if(daiLiHuiyuan.getJb_j()==5){ //代理
			zhuDan.setF1("5"); //
			zhuDan.setF5(daiLiHuiyuan.getId());  
			url = "/management/back/baobiaodaili";
		}
		QiHao qiHao = this.findNewQiHao();
		
		zhuDan.setQh_j(qiHao.getQh_j());
		List<CountBbVO> bbVOs = zhuDanInterface.countRi(qiHao,zhuDan);
		commonCountAllRi(model,bbVOs, qiHao);
		
	    model.addAttribute("qh", qiHao.getQh_j());
		model.addAttribute("kjsj", qiHao.getKjsj_j());
		model.addAttribute("bbVOs", bbVOs);
		
		return url;
	}
	
	private void commonCountAllRi(Model model,List<CountBbVO> bbVOs,QiHao qiHao){
		if(bbVOs.size()>0){
            int i = 0;
			CountBbVO voAll =  new CountBbVO();
			for(CountBbVO vo : bbVOs){
			   voAll.setXh(++i);	
			   voAll.setBs(voAll.getBs()==null?0:voAll.getBs()+(vo.getBs()==null?0:vo.getBs()));
			   voAll.setZcyk(voAll.getZcyk()==null?0:voAll.getZcyk()+(vo.getZcyk()==null?0:vo.getZcyk()));
			   voAll.setZjchi(voAll.getZjchi()==null?0:voAll.getZjchi()+(vo.getZjchi()==null?0:vo.getZjchi()));
			   voAll.setZjje(voAll.getZjje()==null?0:voAll.getZjje()+(vo.getZjje()==null?0:vo.getZjje()));
			   voAll.setZjzs(voAll.getZjzs()==null?0:voAll.getZjzs()+(vo.getZjzs()==null?0:vo.getZjzs()));
			   voAll.setZs(voAll.getZs()==null?0:voAll.getZs()+(vo.getZs()==null?0:vo.getZs()));
			   voAll.setZt(voAll.getZt()==null?0:voAll.getZt()+(vo.getZt()==null?0:vo.getZt()));
			   voAll.setZjzyk(voAll.getZjzyk()==null?0:voAll.getZjzyk()+(vo.getZjzyk()==null?0:vo.getZjzyk()));
			   voAll.setSjzt(voAll.getSjzt()==null?0:voAll.getSjzt()+(voAll.getSjzt()==null?0:vo.getSjzt()));
			   voAll.setSjyk(voAll.getSjyk()==null?0:voAll.getSjyk()+(vo.getSjyk()==null?0:vo.getSjyk()));
			}
		}else{
			bbVOs = null;
		}
		
	}
	
	/**
	 * 跳转到 月报表页面
	 * @return
	 */
	@SuppressWarnings("static-access")
	@RequestMapping("/yuebaobiao")
	public String  yuebaobiao(Model model,HttpServletRequest request){
		
		String url = "";
		ZhuDan zhuDan = new ZhuDan();
		DaiLiHuiyuan daiLiHuiyuan = daiLiHuiyuanInterface.selectByPrimaryKey((Integer) request.getSession().getAttribute(CommonFields.USERID));
		if(daiLiHuiyuan.getJb_j()==1){ //总监
		
		}else if(daiLiHuiyuan.getJb_j()==2){ //大股东
 
		}else if(daiLiHuiyuan.getJb_j()==3){ //股东
			zhuDan.setF1("3");
			zhuDan.setF5(daiLiHuiyuan.getId());
		}else if(daiLiHuiyuan.getJb_j()==4){ //总代理
			zhuDan.setF1("4"); //f1用于判断是那种用户
			zhuDan.setF6(1);
			zhuDan.setF5(daiLiHuiyuan.getId());  
			url = "/management/back/yuebaobiao";
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Calendar cal=Calendar.getInstance(); 
			String yearMonth = request.getParameter("yearMonth");
			String state = request.getParameter("state");
			if(yearMonth!=null&&state!=null&&"false".equals(state)){
				model.addAttribute("yearMonth", sdf.format(cal.getTime()));
			    model.addAttribute("state", "true");
				zhuDan.setF2(request.getParameter("lastMonth")+"-01");
				cal.set(cal.DAY_OF_MONTH,-1);
				zhuDan.setF3(yearMonth+"-"+cal.getActualMaximum(cal.DAY_OF_MONTH));
			}else{
				String nowDate1 = sdf.format(cal.getTime());
				zhuDan.setF2(nowDate1+"-01");
				zhuDan.setF3(nowDate1+"-"+cal.getActualMaximum(cal.DAY_OF_MONTH));
		        cal.set(cal.DAY_OF_MONTH,-1);
		        Date d = cal.getTime();
		        model.addAttribute("yearMonth", sdf.format(d));
		        model.addAttribute("state", "false");
			}
			List<CountBbVO> ybbVos = zhuDanInterface.countYue(zhuDan);
			commonCountAllYue(model,ybbVos);
			model.addAttribute("jb", 4);
		}else if(daiLiHuiyuan.getJb_j()==5){ //代理
			zhuDan.setF1("5"); //
			zhuDan.setF5(daiLiHuiyuan.getId());  
			url = "/management/back/yuebaobiaodaili";
			model.addAttribute("jb", 5);
		}
		
		
		return url;
	}
	
	private  void commonCountAllYue(Model model,List<CountBbVO> ybbVos) {
		
		//合计
		//会员和代理
		CountBbVO allYbbVO = new CountBbVO();
		model.addAttribute("ybbVos", ybbVos);
		model.addAttribute("allYbbVo", allYbbVO);
		
	}
	
	/**
	 * 跳转到 月报表页面的详细页面
	 * @return
	 */
	@RequestMapping("/xiangxiYueBaobiao")
	public String xiangxiYueBaobiao(Model model , HttpServletRequest request){
		String url = "";
		String qh = request.getParameter("qh");
		List<CountBbVO> ybbVos = new ArrayList<CountBbVO>();
		ZhuDan zhuDan = new ZhuDan();
		zhuDan.setQh_j(Integer.parseInt(qh));
		DaiLiHuiyuan daiLiHuiyuan = daiLiHuiyuanInterface.selectByPrimaryKey((Integer) request.getSession().getAttribute(CommonFields.USERID));
		if(daiLiHuiyuan.getJb_j()==1){ //总监
		
		}else if(daiLiHuiyuan.getJb_j()==2){ //大股东
 
		}else if(daiLiHuiyuan.getJb_j()==3){ //股东
			zhuDan.setF1("3");
			zhuDan.setF5(daiLiHuiyuan.getId());
		}else if(daiLiHuiyuan.getJb_j()==4){ //总代理
			zhuDan.setF1("4"); //f1用于判断是那种用户
			zhuDan.setF6(2);
			zhuDan.setF5(daiLiHuiyuan.getId());  
			url = "/management/back/yuebaobiaodaili";
		}else if(daiLiHuiyuan.getJb_j()==5){ //代理
			zhuDan.setF1("5"); //
			zhuDan.setF5(daiLiHuiyuan.getId());  
			url = "/management/back/yuebaobiaodaili";
		}
		ybbVos = zhuDanInterface.countYue2(zhuDan);
		
		commonCountAllYue(model, ybbVos);
		model.addAttribute("qh", qh);
		
		return url;
	}
	/**
	 * 月报表    统计代理下的用户
	 * @param request
	 * @return
	 */
	@RequestMapping("/yonghuYueBaobiao")
	public String yonghuYueBaobiao(Model model,HttpServletRequest request){
		ZhuDan zhuDan = new ZhuDan();
		String qh = request.getParameter("qh");
		String dlid = request.getParameter("dlid");
		zhuDan.setF1("5"); //
		zhuDan.setF5(Integer.parseInt(dlid));  
		zhuDan.setQh_j(Integer.parseInt(qh));
		QiHao qiHao = findQiHaoByQh(Integer.parseInt(qh));
		List<CountBbVO>  bbVOs = zhuDanInterface.countRi(qiHao,zhuDan);
		
		commonCountAllRi(model,bbVOs, qiHao);
		
	    model.addAttribute("qh", qh);
		model.addAttribute("kjsj", qiHao.getKjsj_j());
		model.addAttribute("bbVOs", bbVOs);
		
		return "/management/back/yuebaobiaohuiyuan";
	}

	/**
	 * 跳转到 月报表会员页面
	 * @return
	 */
	@RequestMapping("/yuebaobiaohuiyuan")
	public String  yuebaobiaohuiyuan(){
		return "/management/back/yuebaobiaohuiyuan";
	}
	
	/**
	 * 跳转到 月报表会员页面
	 * @return
	 */
	@RequestMapping("/yuebaobiaodaili")
	public String  yuebaobiaodaili(){
		return "/management/back/yuebaobiaodaili";
	}
	
	
	/**
	 * 跳转到 越级操作页面
	 * @return
	 */
	@RequestMapping("/yuejicaozuo")
	public String  yuejicaozuo(){
		return "/management/back/yuejicaozuo";
	}
	
	/**
	 * 跳转到账户列表页面
	 * @return
	 */
	@RequestMapping("/zhanghuliebiao")
	public String  zhanghuliebiao(){
		return "/management/back/zhanghuliebiao";
	}
	
	/**
	 * 跳转到子账户列表页面
	 * @return
	 */
	@RequestMapping("/zizhanghao")
	public String  zizhanghao(){
		return "/management/back/zizhanghao";
	}
	
	/**
	 * 跳转到总货明细页面
	 * @return
	 */
	@RequestMapping("/zonghuomingxi")
	public String  zonghuomingxi(Model model,HttpServletRequest request){
		DaiLiHuiyuan daiLiHuiyuan = daiLiHuiyuanInterface.selectByPrimaryKey((Integer) request.getSession().getAttribute(CommonFields.USERID));
//		if(daiLiHuiyuan.getJb_j()==1){ //总监
//		
//		}else if(daiLiHuiyuan.getJb_j()==2){ //大股东
//			criteria.andDgd_idEqualTo(daiLiHuiyuan.getId());
//		}else if(daiLiHuiyuan.getJb_j()==3){ //股东
//			criteria.andGd_idEqualTo(daiLiHuiyuan.getId());
//		}else if(daiLiHuiyuan.getJb_j()==4){ //总代理
//			criteria.andZdl_idEqualTo(daiLiHuiyuan.getId());
//		}else if(daiLiHuiyuan.getJb_j()==5){ //代理
//			criteria.andDl_idEqualTo(daiLiHuiyuan.getId());
//		}
		ZhuDan zhuDan = new ZhuDan(); 
		String yhm = request.getParameter("s_username");
		String hm = request.getParameter("s_number");
		String xian = request.getParameter("sizixian");
		String yuan1 = request.getParameter("s_money");
		String yuan2 = request.getParameter("s_money_end");
		String lx = request.getParameter("s_classid");
		if(yhm!=null&&!"".equals(yhm)){
			zhuDan.setF1(yhm);
		}
		if(yuan1!=null&&!"".equals(yuan1)&&yuan2!=null&&!"".equals(yuan2)){
			zhuDan.setF7(Float.parseFloat(yuan1));
			zhuDan.setF8(Float.parseFloat(yuan2));
		}
		if(hm!=null&&!"".equals(hm)){
			zhuDan.setHm_j(hm);
		}
		if(xian!=null&&"on".equals(xian)){
			zhuDan.setLx_j("四字现");
		}else if(lx!=null&&!"全部".equals(lx)){
			if(!"二字定".equals(lx)||!"三字定".equals(lx)){
				zhuDan.setLx_j(lx);
			}else{
				zhuDan.setF2(lx);
			}
		}
		List<ZhuDanVo> list = zhuDanInterface.selectList(zhuDan);
		
		List<HaoMaLeiXing> hmlxList = haoMaLeiXingInterface.selectByExample(new HaoMaLeiXingExample());
		if(hmlxList!=null&&hmlxList.size()>0){
			HaoMaLeiXing hmlx1 = new HaoMaLeiXing();
			hmlx1.setLx_j("二字定");
			hmlxList.add(0, hmlx1);
			HaoMaLeiXing hmlx2 = new HaoMaLeiXing();
			hmlx2.setLx_j("三字定");
			hmlxList.add(7, hmlx2);
		}
		
		model.addAttribute("zhuDanVoList", list);
		model.addAttribute("s_username", yhm);
		model.addAttribute("s_number", hm);
		model.addAttribute("sizixian", xian);
		model.addAttribute("s_money", yuan1);
		model.addAttribute("s_money_end", yuan2);
		model.addAttribute("s_classid", lx);
		model.addAttribute("hmlxList", hmlxList);
		
		return "/management/back/zonghuomingxi";
	}
	/**
	 * 查找最新的期号
	 * @return
	 */
	public QiHao findNewQiHao(){
		
		return qiHaoInterface.selectMaxQiHao();
	}
	
	
	/**
	 * 开奖号码
	 * @return
	 */
	@RequestMapping("/kaijianghaoma")
	public String  kaijianghaoma(Model model){
		QiHaoExample qiHaoExample = new QiHaoExample();
		qiHaoExample.setOrderByClause(" id desc");
		 List<QiHao> list =  qiHaoInterface.selectByExample(qiHaoExample);
		 List<QiHaoVo> qiHaoVos = null;
		 if(list!=null&&list.size()>0){
			 model.addAttribute("maxQh", list.get(0).getQh_j()+1);
			 qiHaoVos = new ArrayList<QiHaoVo>();
		 }else{
			 model.addAttribute("maxQh",10000);
		 }
		 for(int i = 0 ; i < list.size() ; i ++){
			 QiHao qiHao = list.get(i);
			 QiHaoVo qiHaoVo =  new QiHaoVo();
			 qiHaoVo.setKjsj(qiHao.getKjsj_j());
			 qiHaoVo.setQh(qiHao.getQh_j());
			 if(qiHao.getKjhm_j()!=null){
				 char ch[] = (qiHao.getKjhm_j()+"").toCharArray();
					qiHaoVo.setQian(ch[0]+"");
					qiHaoVo.setBai(ch[1]+"");
					qiHaoVo.setShi(ch[2]+"");
					qiHaoVo.setGeStr(ch[3]+"");
					qiHaoVo.setQiu5(ch[4]+"");
					qiHaoVo.setQiu6(ch[5]+"");
					qiHaoVo.setQiu7(ch[6]+"");
			 }
			
			qiHaoVos.add(qiHaoVo);
		 }
		model.addAttribute("qiHaoVoList", qiHaoVos);
		return "/management/back/kaijianghaoma";
	}
	
	/**
	 * 规则说明
	 * @return
	 */
	@RequestMapping("/guizeshuoming")
	public String  guizeshuoming(){
		return "/management/back/guizeshuoming";
	}
	
	
	/**
	 * 查看
	 * @return
	 */
	@RequestMapping("/chakan")
	public String  chakan(){
		return "/management/back/chakan";
	}
	
	/**
	 * 代理账户编辑
	 * @return
	 */
	@RequestMapping("/dailizhanghubianji")
	public String  dailizhanghubianji(){
		return "/management/back/dailizhanghubianji";
	}
	
	/**
	 * 打印
	 * @return
	 */
	@RequestMapping("/dayin")
	public String  dayin(){
		return "/management/back/dayin";
	}
	
	/**
	 * 打印代理
	 * @return
	 */
	@RequestMapping("/dayindaili")
	public String  dayindaili(){
		return "/management/back/dayindaili";
	}
	
	/**
	 * 打印会员
	 * @return
	 */
	@RequestMapping("/dayinhuiyuan")
	public String  dayinhuiyuan(){
		return "/management/back/dayinhuiyuan";
	}
	
	/**
	 * 打印总货明细
	 * @return
	 */
	@RequestMapping("/dayinzonghuomingxi")
	public String  dayinzonghuomingxi(){
		return "/management/back/dayinzonghuomingxi";
	}
	
	/**
	 * 分类帐
	 * @return
	 */
	@RequestMapping("/fenleizhang")
	public String  fenleizhang(){
		return "/management/back/fenleizhang";
	}
	
	/**
	 * 贡献度
	 * @return
	 */
	@RequestMapping("/gongxiandu")
	public String  gongxiandu(){
		return "/management/back/gongxiandu";
	}

}
